import 'dart:convert';

import 'ac_login_model.dart';
import 'ac_media_model.dart';

T? asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class AcVideoModel {
  AcVideoModel({
    required this.name,
    required this.followers,
    required this.video,
    required this.text,
    required this.isLiked,
    required this.comment,
    this.userInfo,
    this.mediaModel,
  });

  factory AcVideoModel.fromJson(Map<String, dynamic> json) {
    final List<String>? comment = json['comment'] is List ? <String>[] : null;
    if (comment != null) {
      for (final dynamic item in json['comment']!) {
        if (item != null) {
          comment.add(asT<String>(item)!);
        }
      }
    }
    return AcVideoModel(
      name: asT<String>(json['name'])!,
      followers: asT<int>(json['followers'])!,
      video: asT<String>(json['video'])!,
      text: asT<String>(json['text'])!,
      isLiked: asT<bool>(json['isLiked'])!,
      comment: comment!,
      userInfo: json['userInfo'] == null
          ? null
          : Userinfo.fromJson(asT<Map<String, dynamic>>(json['userInfo'])!),
      mediaModel: json['mediaModel'] == null
          ? null
          : AcMediaModel.fromJson(
              asT<Map<String, dynamic>>(json['mediaModel'])!),
    );
  }

  String name;
  int followers;
  String video;
  String text;
  bool isLiked;
  List<String> comment;
  Userinfo? userInfo;
  AcMediaModel? mediaModel;

  @override
  String toString() {
    return jsonEncode(this);
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'name': name,
        'followers': followers,
        'video': video,
        'text': text,
        'isLiked': isLiked,
        'comment': comment,
        'userInfo': userInfo,
        'mediaModel': mediaModel,
      };
}
